﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VolunteerHubAdapter.ViewModels;
using NUnit.Framework;
namespace VolunteerHubAdapter.ViewModels.Tests
{
    [TestFixture()]
    public class SettingsViewModelTests
    {
        [Test()]
        public void SettingsViewModelConstructorWithNullArgumentTest()
        {
            SettingsViewModel vm= null;

            Assert.Throws<ArgumentNullException>(delegate() { vm = new SettingsViewModel(null); });
        }

        [Test()]
        public void IsEnabledFalseUnlessAllValuesSet()
        {
            Models.Settings model = new Models.Settings();
            SettingsViewModel vm = new SettingsViewModel(model);

            Assert.True(vm.IsEnabled == false);
        }

        [Test()]
        public void IsEnabledTrueWhenAllValuesSet()
        {
            Models.Settings model = new Models.Settings()
            {
                LastRunDate = new DateTime(2013,12,31),
                Password = "Password",
                URL = "URL",
                User = "User"
            };

            SettingsViewModel vm = new SettingsViewModel(model);

            Assert.True(vm.IsEnabled == true);
        }

        [Test()]
        public void TestConnectionTest()
        {
            string password = "";
            string url = "https://yourorg.volunteerhub.com";
            string user = "";

            Models.Settings model = new Models.Settings()
            {
                Password = password,
                URL = url,
                User = user
            };

            SettingsViewModel vm = new SettingsViewModel(model);

            Assert.DoesNotThrow(delegate() { vm.TestConnection(); });
        }

        [Test()]
        public void SaveTest()
        {
            Models.EventQueryType queryType = Models.EventQueryType.ByDate;
            DateTime startDate=new DateTime(2014, 1, 1);
            DateTime lastRunDate = new DateTime(2013, 12, 31);
            string password = "Password";
            string url = "URL";
            string user = "User";

            Models.Settings model = new Models.Settings()
            {
                LastRunDate = lastRunDate,
                Password = password,
                URL = url,
                User = user
            };

            SettingsViewModel vm = new SettingsViewModel(model);

            vm.LastRunDate = lastRunDate.AddMonths(1);
            vm.Password = Reverse(password);
            vm.URL = Reverse(url);
            vm.User = Reverse(user);
            vm.Save();

            Assert.True(model.LastRunDate == lastRunDate.AddMonths(1));
            Assert.True(model.Password == Reverse(password));
            Assert.True(model.URL == Reverse(url));
            Assert.True(model.User == Reverse(user));
        }

        private static string Reverse(string s)
        {
            char[] charArray = s.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
        }
    }
}
